package com.yfy.web;


import com.alibaba.fastjson.JSON;
import com.yfy.pojo.User;
import com.yfy.service.UserService;
import com.yfy.service.UserServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet{

    private UserService service=new UserServiceImpl();

    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        req.setCharacterEncoding("utf-8");
        BufferedReader br = req.getReader();
        String params = br.readLine();
        User user = JSON.parseObject(params, User.class);
        //String username = req.getParameter("username");
        //String password = req.getParameter("password");
        //User user = new User();
        //user.setUsername(username);
        //user.setPassword(password);
        User u = service.login(user.getUsername(), user.getPassword());
        if(u!=null){
            resp.getWriter().write("success");
        }else {
            resp.getWriter().write("error");
        }
    }
    public void register(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader br = req.getReader();
        String params = br.readLine();

        User user = JSON.parseObject(params, User.class);
        boolean register = service.register(user);
        if(register){
            resp.getWriter().write("success");
        }
    }


}
